/*
   电子钟&高考倒计时
   作者：WonderBoy
   最后修改日期：2020-10-11
*/
#include <RTC.h>
#include <Adafruit_SSD1306.h>
#include "getday.h"

static DS3231 RTC;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);

String addZero(int raw) {
  if (raw <= 9)
    return "0" + String(raw);
  else
    return String(raw);
}

void setup() {
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.setTextColor(WHITE);
}

void loop()
{
  oled.clearDisplay();

  oled.setTextSize(1);
  oled.setCursor(25, 10);
  oled.print(String(RTC.getYear()) + "-" + String(RTC.getMonth()) + "-" + String(RTC.getDay()) + " ");
  switch (RTC.getWeek())
  {
    case 1:
      oled.print("SUN");
      break;
    case 2:
      oled.print("MON");
      break;
    case 3:
      oled.print("TUE");
      break;
    case 4:
      oled.print("WED");
      break;
    case 5:
      oled.print("THU");
      break;
    case 6:
      oled.print("FRI");
      break;
    case 7:
      oled.print("SAT");
      break;
  }

  oled.setTextSize(2);
  oled.setCursor(16, 25);
  oled.println(addZero(RTC.getHours()) + ":" + addZero(RTC.getMinutes()) + ":" + addZero(RTC.getSeconds()));

  oled.setTextSize(1);
  oled.setCursor(40, 48);
  float f = getDays(RTC.getYear(), RTC.getMonth(), RTC.getDay()) + getPartDay(RTC.getHours(), RTC.getMinutes(), RTC.getSeconds());
  char c[15];
  unsigned int len = String((int)f).length();
  dtostrf(f, len, 5, c); 
  oled.print(c);

  oled.display();
  delay(500);
}
